<?php
namespace App\Http\Controllers;

use App\Models\Admin_group;
use App\Models\Admin_group_member;
use App\Models\User;


use Request, Config, Session, Validator, Hash, Res;
class ManagerController extends PublicController {    
    public function _initialize(){
    }
    
    public function anyList(){
        $result = [];
        $member_list = Admin_group_member::leftJoin('users', 'admin_group_member.user_id', '=', 'users.id')
                ->leftJoin('admin_group', 'admin_group_member.group_id', '=', 'admin_group.group_id')
                ->orderBy('admin_group_member.created_at','desc')
                ->select('admin_group_member.id', 'admin_group_member.user_id', 'users.mobile', 'admin_group.groupname')
                ->paginate(15);

        $result['member_list'] = $member_list;
        
        return view('admin.manager.list')->with('result', $result);
    }
    
    public function anyAdd(){
        if (!Request::has('flag')) {
            $result = [];
            $group_list = Admin_group::orderBy('group_id','desc')->get();
            $result['group_list'] = $group_list;
//            var_dump($group_list);exit;
            return view('admin.manager.add')->with('result', $result);
        }

        $validator = Validator::make(Request::all(), array(
                    'mobile' => 'required|digits:11',
                    'group_id' => 'required|numeric',
        ));
        if ($validator->fails()) {
            return Res::showMessage('error',$validator->errors(),Session::get('admin.currentRoute'));
        }
        $params=Request::all();        
        $user = User::where('mobile', $params['mobile'])->first();
        if(empty($user)){
            return Res::showMessage('error','手机号码未注册',Session::get('admin.currentRoute'));
        }
        $params['user_id'] = $user->id;
        $group = Admin_group::where('group_id', $params['group_id'])->first();
        if(empty($group)){
            return Res::showMessage('error','违规操作',Session::get('admin.currentRoute'));
        }
        if (!$admin_group_member = Admin_group_member::create($params)) {
            return Res::showMessage('error','添加管理员失败',Session::get('admin.currentRoute'));
        }
        return redirect('manager/list');
    }
    
    public function anyEdit(){
        $validator = Validator::make(Request::all(), array(
                    'id' => 'required|numeric',
        ));
        if ($validator->fails()) {
            return Res::showMessage('error', $validator->errors(), 'group/list');
        }
        $id = Request::get('id');
        $group_member = Admin_group_member::where('id', $id)->first();
        $admin_group_member = Admin_group_member::leftJoin('users', 'admin_group_member.user_id', '=', 'users.id')
                ->leftJoin('admin_group', 'admin_group_member.group_id', '=', 'admin_group.group_id')
                ->where('admin_group_member.id', $id)
                ->select('admin_group_member.id', 'admin_group_member.user_id', 'users.mobile', 'admin_group.group_id')
                ->first();
        if(empty($admin_group_member)){
            return Res::showMessage('error','违规操作','manager/list');
        }
        
        if (!Request::has('flag')) {
            $result = [];
            $result['admin_group_member'] = $admin_group_member;
            $group_list = Admin_group::orderBy('group_id','desc')->get();
            $result['group_list'] = $group_list;
            return view('admin.manager.edit')->with('result', $result);
        }

        $validator = Validator::make(Request::all(), array(
                    'group_id' => 'required|numeric',
        ));
        if ($validator->fails()) {
            return Res::showMessage('error',$validator->errors(),Session::get('admin.currentRoute').'?id='.$id);
        }
        $params=Request::all();
        $group_member->group_id = $params['group_id'];
        if (!$group_member->save()) {
            return Res::showMessage('error','编辑管理员失败',Session::get('admin.currentRoute').'?id='.$id);
        }
        return redirect('manager/list');
    }
    
    public function anyDel(){
        $validator = Validator::make(Request::all(), array(
                    'id' => 'required|numeric',
        ));
        if ($validator->fails()) {
            return Res::showMessage('error', $validator->errors(), 'group/list');
        }
        $id = Request::get('id');
        Admin_group_member::where('id',$id)->delete();
        return redirect('manager/list');
    }
}
